package com.magicsoft.share.adapter.article;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.magicsoft.share.R;
import com.magicsoft.share.bean.article.ArticleContent;

import java.util.ArrayList;

/**
 * Created by lyf on 2017/11/10/010.
 */

public class AddArticleAdapter extends RecyclerView.Adapter<AddArticleAdapter.MyViewHolder> {


    private final Context mContext;
    private final TranslateAnimation mShowAction1;
    private final TranslateAnimation mShowAction2;
    private final TranslateAnimation mHiddenAction;
    private OnItemImageClickListener onItemImageClickListener;
    private OnItemTextClickListener onItemTextClickListener;
    private OnItemAddTextClickListener onItemAddTextClickListener;
    private OnItemAddImageClickListener onItemAddImageClickListener;
    private OnItemDeleteClickListener onItemDeleteClickListener;

    private ArrayList<ArticleContent> mList = new ArrayList<>();

    public AddArticleAdapter(Context context) {
        mContext = context;
        mShowAction1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.5f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowAction1.setDuration(500);
        mShowAction2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.5f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowAction2.setDuration(500);

        mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        mHiddenAction.setDuration(500);


    }

    public ArrayList<ArticleContent> getmList() {
        return mList;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_edt_article, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        Glide.with(mContext).load(mList.get(position).getImg()).into(holder.imageView);
        Log.d("lyf", "onBindViewHolder: "+mList.get(position).getImg());
        holder.tvContent.setText(mList.get(position).getContent());
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null == onItemImageClickListener) {
                    return;
                }
                onItemImageClickListener.onClick(position);
            }
        });

        holder.tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null == onItemTextClickListener) {
                    return;
                }
                onItemTextClickListener.onClick(position);
            }
        });
        holder.ivAddText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null == onItemAddTextClickListener) {
                    return;
                }
                onItemAddTextClickListener.onClick(position);
            }
        });
        holder.ivAddImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null == onItemAddImageClickListener) {
                    return;
                }
                onItemAddImageClickListener.onClick(position);
            }
        });

        holder.iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null == onItemDeleteClickListener) {
                    return;
                }
                onItemDeleteClickListener.onClick(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addData(ArrayList<ArticleContent> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void setOnItemImageClickListener(OnItemImageClickListener onItemImageClickListener) {
        this.onItemImageClickListener = onItemImageClickListener;
    }

    public void setOnItemTextClickListener(OnItemTextClickListener onItemTextClickListener) {
        this.onItemTextClickListener = onItemTextClickListener;
    }

    public void setOnItemAddImageClickListener(OnItemAddImageClickListener onItemAddImageClickListener) {
        this.onItemAddImageClickListener = onItemAddImageClickListener;
    }

    public void setOnItemAddTextClickListener(OnItemAddTextClickListener onItemAddTextClickListener) {
        this.onItemAddTextClickListener = onItemAddTextClickListener;
    }


    public void refreshContent(int position, String contentStr) {

        mList.get(position).setContent(contentStr);
        this.notifyItemChanged(position);
    }

    public void addContentItem(int position, String contentStr, String Img) {
        ArticleContent articleContent = new ArticleContent();
        articleContent.setContent(contentStr);
        articleContent.setImg(Img);
        mList.add(position + 1, articleContent);
        notifyItemInserted(position + 1);//通知演示插入动画
        notifyItemRangeChanged(position, mList.size() - position);//通知数据与界面重新绑定
        notifyDataSetChanged();
    }

    public void deleteItem(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mList.size() - position);//通知数据与界面重新绑定
    }

    public void setOnItemDeleteClickListener(OnItemDeleteClickListener onItemDeleteClickListener) {
        this.onItemDeleteClickListener = onItemDeleteClickListener;
    }

    public interface OnItemImageClickListener {
        void onClick(int position);

    }

    public interface OnItemTextClickListener {
        void onClick(int position);

    }

    public interface OnItemAddTextClickListener {
        void onClick(int position);

    }

    public interface OnItemAddImageClickListener {
        void onClick(int position);

    }

    public interface OnItemDeleteClickListener {
        void onClick(int position);

    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView ivAdd;
        private ImageView ivAddImg;
        private ImageView iv_close;
        private ImageView ivAddText;
        private ImageView imageView;
        private TextView tvContent;
        private boolean isShow;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivAdd = (ImageView) itemView.findViewById(R.id.iv_add);
            iv_close = (ImageView) itemView.findViewById(R.id.iv_close);
            ivAddImg = (ImageView) itemView.findViewById(R.id.iv_add_img);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            ivAddText = (ImageView) itemView.findViewById(R.id.iv_add_text);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            ivAdd.setOnClickListener(this);
            ivAddImg.setOnClickListener(this);
            ivAddText.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_add:
                    if (!isShow) {
                        ivAdd.animate().rotation(45);
                        ivAddImg.startAnimation(mShowAction2);
                        ivAddImg.setVisibility(View.VISIBLE);
                        ivAddText.startAnimation(mShowAction1);
                        ivAddText.setVisibility(View.VISIBLE);
                        isShow = true;

                    } else {
                        ivAdd.animate().rotation(180);
                        ivAddImg.startAnimation(mHiddenAction);
                        ivAddImg.setVisibility(View.INVISIBLE);
                        ivAddText.startAnimation(mHiddenAction);
                        ivAddText.setVisibility(View.INVISIBLE);
                        isShow = false;

                    }
                    break;
                case R.id.iv_add_img:
                    break;
                case R.id.iv_add_text:
                    break;
            }

        }
    }
}
